package cn.kgc.controller;

import cn.hutool.core.map.MapUtil;
import cn.kgc.entity.Dept;
import cn.kgc.entity.Emp;
import cn.kgc.service.DeptService;
import cn.kgc.service.EmpService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/sys")
public class EmpController {

    @Autowired
    private EmpService empService;
    @Autowired
    private DeptService deptService;

    @PostMapping("/login")
    public String login(String empname, String password, HttpSession session, RedirectAttributes model){
        QueryWrapper<Emp> wrapper = new QueryWrapper<Emp>();
        wrapper.eq("empName",empname)
               .eq("password",password);
        Emp emp = empService.getOne(wrapper);

        if(emp == null){
           model.addFlashAttribute("error","用户名或密码错误！");
           return "redirect:/toLogin";
        }

        session.setAttribute("loginEmp",emp);
        return "redirect:/index";
    }

    @GetMapping("/empList")
    public String empList(@RequestParam(defaultValue = "1") Integer pageId,
                          @RequestParam(defaultValue = "5") Integer pageSize,
                          Model model){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        Page<Emp> pageInfo = new Page<>(pageId,pageSize);
        empService.page(pageInfo,wrapper);

        //遍历查询结果，填充每个用户的dept属性值
        pageInfo.getRecords().stream().forEach(emp -> emp.setDept(deptService.getById(emp.getDeptId())));

        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

    @GetMapping("/emp")
    public String toEmpAdd(Model model) {
        List<Dept> deptList = deptService.list();
        model.addAttribute("deptList", deptList);
        return "add";
    }

    @PostMapping("/emp")
    public String empAdd(Emp emp) {
        emp.setPassword("266543");
        empService.save(emp);
        return "redirect:/sys/empList";
    }

    @GetMapping("/emp/{empId}")
    public String toEmpModify(@PathVariable Integer empId, Model model) {
        List<Dept> deptList = deptService.list();
        Emp emp =empService.getById(empId);

        model.addAttribute("deptList", deptList);
        model.addAttribute("emp", emp);
        return "add";
    }

    @PutMapping("/emp")
    public String empModify(Emp emp) {
        empService.updateById(emp);

        return "redirect:/sys/empList";
    }

    @DeleteMapping("/emp/{empId}")
    @ResponseBody
    public Map empRemove(@PathVariable("empId")         Integer empId) {
        Boolean ok = empService.removeById(empId);

        return MapUtil.builder()
                .put("ok",ok)
                .build();
    }



}
